﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ReplaceCsv.WallLogParser
{
    class WallLogAvgOutput
    {
        private string _outputFileName;
        public string outputFileName
        {
            get { return _outputFileName; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="outputFileName"></param>
        public WallLogAvgOutput(string outputFileName)
        {
            this._outputFileName = outputFileName;
        }
        public bool output(Dictionary<string, Dictionary<string, List<WallLogBean>>> input)
        {
            //まずは出力ファイルを初期化
            bool result = false;
            init();
            //一旦wall->日時(ファイル名）の順で出力を行う。
            List<string> ar = WallLogTool.getKeyList(input);
            ar.Sort();
            //20130315 ヘッダー出力を追加
            printHeader();
            foreach (string key in ar)
            {
                Dictionary<string, List<WallLogBean>> now = input[key];
                List<string> fn = WallLogTool.getKeyList(now);
                fn.Sort();
                foreach (string key2 in fn)
                {
                    printOut(key,now[key2]);
                }
            }

            result = true;
            return result;
        }
        /// <summary>
        /// ファイル書き込み
        /// </summary>
        /// <param name="filename"></param>
        private void init(){
            try
            {
                File.Delete(this.outputFileName);
            }
            catch (Exception ex)
            {
            }
        }
        private void printHeader()
        {
                        //ちゃんと後でExceptionの処理を付け加えておくこと 20130225
            using (TextWriter outCsv = new StreamWriter(this.outputFileName, true, Encoding.GetEncoding(932)))
            {
                outCsv.WriteLine("WALL名,サマリー対象時間,コンテンツ名,滞在時間,抽出範囲");
                outCsv.Flush();
                outCsv.Close();
            }
        }
        private void printOut(string wall , List<WallLogBean> inputList){
            //ちゃんと後でExceptionの処理を付け加えておくこと 20130225
            using (TextWriter outCsv = new StreamWriter(this.outputFileName, true, Encoding.GetEncoding(932)))
            {
                foreach(WallLogBean bean in inputList){
                    //出力フラグを確認するように変更 20130319
                    if (!bean.isOutputedAvg) continue;
                    outCsv.WriteLine(getRow(wall, bean));
                }
                outCsv.Flush();
                outCsv.Close();
            }
        }
        private string getRow(string wall,WallLogBean bean){
            string[] results = new string[5];
            results[0] = wall;
            results[1] = bean.accessDateTime.ToString("yyyy年MM月dd日 HH時mm分ss秒");
            results[2] = bean.pageName;
            results[3] = bean.OnTime.ToString();
            results[4] = bean.timeCategory;
            return string.Join(",", results);
        }
    }
}
